/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.pedagogue.core.module;

import javax.persistence.EntityManager;
import org.letmeshare.pedagogue.core.entities.Student;
import org.letmeshare.pedagogue.core.validation.BusinessValidationException;

/**
 *
 * @author ibisuser1
 */
public class StudentModule extends AbstractModule {
    
    public static void createStudent (EntityManager em, Student student) {
        checkEntityManager(em);
        if (student == null) {
            throw new IllegalArgumentException("Student required");
        }
        student.validate();
        em.persist(student);
    }
    
    public static void updateProfile (EntityManager em, Student student) {
        checkEntityManager(em);
        if (student == null) {
            throw new IllegalArgumentException("Student required");
        }
        Student oldStudent = getStudent(em, student.getUid());
        if (oldStudent == null) {
            throw new BusinessValidationException("Student does not exist");
        }
        student.setRoles(oldStudent.getRoles());
        student.validate();
        em.merge(student);
    }
    
    public static Student getStudent (EntityManager em, String studId) {
        checkEntityManager(em);
        if (studId == null) {
            throw new IllegalArgumentException("StudentId required");
        }
        return em.find(Student.class, studId);
    }
}
